package com.example.demo01.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo01.entity.User;
import com.example.demo01.exception.ZgcError;
import com.example.demo01.exception.ZgcException;
import com.example.demo01.mapper.UserMapper;
import com.example.demo01.request.GetOneRequest;
import com.example.demo01.request.GetUserListRequest;
import com.example.demo01.utils.POIUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> getUserList(GetUserListRequest getUserListRequest) {
        return userMapper.getUserList(getUserListRequest);
    }

    @Override
    public void exportUserList() {
        //1.获取要导出数据
        List<User> userList = userMapper.getAllUser();
        try {
            POIUtil.exportExcel(userList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public User getOneUser(GetOneRequest request) {
        //前置校验
        if (request == null) {
            throw new ZgcException(ZgcError.QUERY_OBJECT_IS_NULL);
        } else {
            if (request.getUserId() == null || "".equals(request.getUserId())) {
                log.error(new ZgcException(ZgcError.QUERY_USER_IS_NOT_EXIST).getErrorMsg());
                throw new ZgcException(ZgcError.QUERY_PARAM_IS_NULL);
            }
        }
        User user = userMapper.getOneByUserId(request.getUserId());
        //后置校验
        if (user == null) {
            throw new ZgcException(ZgcError.QUERY_USER_IS_NOT_EXIST);
        }
        return user;
    }
}
